package com.example.webbrowser;

import android.database.Cursor;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleCursorAdapter;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.webbrowser.Fragment.BookMarkFragment;
import com.example.webbrowser.Fragment.HistoryFragment;
import com.example.webbrowser.Fragment.HomeFragment;


public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup rg_tab_bar;
    private RadioButton rb_home;

    private HomeFragment fg1;
    private BookMarkFragment fg2;
    private HistoryFragment fg3;
    private FragmentManager fManager;
    private MyTouchListener myTouchListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        setContentView(R.layout.activity_main);
        //initData(savedInstanceState);
        fManager = getSupportFragmentManager();
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        rg_tab_bar.setOnCheckedChangeListener(this);
        //设置第一个按钮为选中状态
        rb_home = (RadioButton) findViewById(R.id.rb_home);
        rb_home.setChecked(true);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkedId) {
            case R.id.rb_home:
                if (fg1 == null) {
                    fg1 = new HomeFragment();
                    fTransaction.add(R.id.ly_content, fg1);
                } else {
                    fTransaction.show(fg1);
                }
                break;
            case R.id.rb_bookmark:
                if (fg2 == null) {
                    fg2 = new BookMarkFragment();
                    fTransaction.add(R.id.ly_content, fg2);
                } else {
                    fTransaction.show(fg2);
                }
                break;
            case R.id.rb_history:
                if (fg3 == null) {
                    fg3 = new HistoryFragment();
                    fTransaction.add(R.id.ly_content, fg3);
                } else {
                    fTransaction.show(fg3);
                }
                break;
        }
        fTransaction.commit();
    }

    //隐藏没有被选中的Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (fg1 != null) fragmentTransaction.hide(fg1);
        if (fg2 != null) fragmentTransaction.hide(fg2);
        if (fg3 != null) fragmentTransaction.hide(fg3);
    }



    /************导航栏新增功能************/
    //从其他Activity跳转到MainActivity下的任意一个Fragment
    @Override
    protected void onResume() {
        // 获取从其他Activity发送过来跳转Fragment的标志fragment_flag
        int fragmentFlag = this.getIntent().getIntExtra("fragment_flag", 0);
        switch (fragmentFlag){
            case 1:
                rg_tab_bar.check(R.id.rb_home);
                break;
            case 2:
                rg_tab_bar.check(R.id.rb_bookmark);
                break;
            case 3:
                rg_tab_bar.check(R.id.rb_history);
                break;
        }
        super.onResume();
    }

    /*@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            point.x = (int) ev.getRawX();
            point.y = (int) ev.getRawY();
        }
        return super.dispatchTouchEvent(ev);
    }
    public interface PointEvent{
        public void getPoint(MotionEvent ev);
    }
    public void setPointEvent(PointEvent event){
        this.pointEvent = event;
    }*/
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //将触摸事件传递给回调函数
        if (null != myTouchListener) {
            myTouchListener.onTouch(ev);
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 用于注册回调事件
     */
    public void registerMyTouchListener(MyTouchListener myTouchListener) {
        this.myTouchListener = myTouchListener;
    }

    /**
     * 定义一个接口
     * @author fox
     *
     */
    public interface MyTouchListener {
        public void onTouch(MotionEvent ev);
    }
}